﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;

namespace DataFileApp
{
    public class LanguageConverter
    {


        public void changeLanguage(Form form, string cultureCode)
        {

            ResourceManager rm = new ResourceManager("DataFileApp.Lang.Strings", typeof(Form1).Assembly);
            CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);
            // Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
            System.Reflection.FieldInfo[] fieldInfo = form.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            for (int i = 0; i < fieldInfo.Length; i++)
            {
                switch (fieldInfo[i].FieldType.Name)
                {
                    case "ContextMenuStrip":
                        ContextMenuStrip contextMenuStrip = (ContextMenuStrip)fieldInfo[i].GetValue(form);
                        updateControlText(contextMenuStrip,rm);
                        break;
                }
            }
            updateControlText(form, rm);

           
        }


        public void updateControlText(Control control, ResourceManager rm)
        {
            if (rm.GetString(control.Name) != null)
            {
                control.Text = rm.GetString(control.Name);
            }

            foreach (Control child in control.Controls)
            {
                updateControlText(child, rm);
            }

            if (control is MenuStrip menuStrip)
            {
                foreach (ToolStripMenuItem item in menuStrip.Items)
                {
                    updateMenuUtems(item, rm);
                }
            }
            if (control is StatusStrip statusStrip)
            {
                foreach (ToolStripItem item in statusStrip.Items)
                {
                   updateMenuUtems(item, rm);
                }
            }
            if (control is DataGridView dataGridView)
            {
                dataGridView.Columns[0].HeaderText = rm.GetString("dataGridView.Columns[0]");
                dataGridView.Columns[1].HeaderText = rm.GetString("dataGridView.Columns[1]");
                dataGridView.Columns[2].HeaderText = rm.GetString("dataGridView.Columns[2]");
            }
            //contextMenuStrip
            if (control is ContextMenuStrip context)
            {
                foreach (ToolStripItem item in context.Items)
                {
                    updateMenuUtems(item, rm);
                }
            }
        }


        public void updateMenuUtems(ToolStripMenuItem menuItem, ResourceManager rm)
        {
            if (rm.GetString(menuItem.Name) != null)
            {
                menuItem.Text = rm.GetString(menuItem.Name);
            }

            foreach (ToolStripItem item in menuItem.DropDownItems)
            {
                if (item is ToolStripMenuItem subMenuItem)
                {
                    updateMenuUtems(subMenuItem, rm);
                }
            }
        }
        public void updateMenuUtems(ToolStripItem menuItem, ResourceManager rm)
        {
            if (rm.GetString(menuItem.Name) != null)
            {
                menuItem.Text = rm.GetString(menuItem.Name);
            }
        }
    }
}
